package me.linczh.bank.action;

import java.util.List;

import me.linczh.bank.bean.AccountBean;
import me.linczh.bank.bean.OperatorBean;
import me.linczh.bank.bean.enums.ResultType;
import me.linczh.bank.service.AccountService;
import me.linczh.bank.service.OperatorService;
import me.linczh.framework.action.ActionExecutor;

public class OperatorAction extends ActionExecutor {
	private String account;
	private String password;
	private String username;
	private String newpassword;
	private String identity;
	
	private List<OperatorBean> operators;
	private ResultType result;
	
	public String create() {
		result = null;
		AccountService accountService = new AccountService();
		AccountBean accountBean = accountService.getAccountByIdAndPassword(account, password);
		if (accountBean == null) result = ResultType.INVALID_ACCOUNT_OR_PWD;
		else {
			OperatorService operatorService = new OperatorService();
			List<OperatorBean> list = operatorService.getOperatorByAccount(account);
			for (OperatorBean o : list) {
				if (o.getIdentity().equals(identity) || o.getUsername().equals(username)) {
					result = ResultType.INVALID_ACCOUNT_OR_PWD;
					break;
				}
			}
			if (result == null) {
				operatorService.create(accountBean, identity, username, newpassword);
				result = ResultType.SUCCESS;
			}
		}
		return "success";
	}
	
	public String query() {
		AccountService accountService = new AccountService();
		AccountBean accountBean = accountService.getAccountByIdAndPassword(account, password);
		if (accountBean == null) result = ResultType.INVALID_ACCOUNT_OR_PWD;
		else {
			OperatorService operatorService = new OperatorService();
			operators = operatorService.getOperatorByAccount(account);
			//result = ResultType.SUCCESS;
		}
		return "success";
	}
	
	public String delete() {
		AccountService accountService = new AccountService();
		AccountBean accountBean = accountService.getAccountByIdAndPassword(account, password);
		if (accountBean == null) result = ResultType.INVALID_ACCOUNT_OR_PWD;
		else {
			OperatorService operatorService = new OperatorService();
			operatorService.delete(account, identity);
			result = ResultType.SUCCESS;
		}
		return "success";
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public String getIdentity() {
		return identity;
	}

	public void setIdentity(String identity) {
		this.identity = identity;
	}

	public List<OperatorBean> getOperators() {
		return operators;
	}

	public void setOperators(List<OperatorBean> operators) {
		this.operators = operators;
	}

	public ResultType getResult() {
		return result;
	}

	public void setResult(ResultType result) {
		this.result = result;
	}
	
}
